package com.bugRecord.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class AccessBugRecordDao implements IBugRecordDao {

	public String add(Object obj) {
		String sql = (String)obj;
		IConnectionManager cm = ConnectionFactory.createConnectionInstance("access");
		Connection con =  cm.getConnection();
		try {
			Statement st = con.createStatement();
			st.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return "false";
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return "success";
	}
	public String delete(Object obj) {
		return null;
	}
	public String modify(Object obj) {
		return null;
	}
	public List<Map> selectAll(Object obj) {
		String sql = (String)obj;
		List<Map> v = new Vector<Map>();
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		IConnectionManager cm = ConnectionFactory.createConnectionInstance("access");
		Connection con =  cm.getConnection();
		try {
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			ResultSetMetaData rsmd = rs.getMetaData();
			int columnNum = rsmd.getColumnCount();
			while (rs.next()) {
                Map map = new HashMap();
                for (int i=1; i<=columnNum; i++) {
                    map.put(rsmd.getColumnName(i),rs.getObject(i));
                }
                v.add(map);
            }
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally{
			try {
				rs.close();
				pstmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return v;
	}
	public Map selectById(Object obj) {
		return null;
	}
	

}
